package com.huli.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.huli.dao.UserCouponMapper;
import com.huli.domain.Coupon;
import com.huli.domain.User;
import com.huli.domain.UserCoupon;
import com.huli.exception.CustomException;
import com.huli.feign.UserClient;
import com.huli.service.CouponService;
import com.huli.service.UserCouponService;
import com.huli.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@RequestMapping("coupon")
@Api(tags = "优惠券服务")
public class CouponController {

    @Autowired
    CouponService couponService;

    @Autowired
    UserCouponService userCouponService;

    @Autowired
    UserClient userClient;

    /**
     * 通过优惠券Id 获取优惠券详情
     * @param cid
     * @return
     */
    @GetMapping("getCoupon/{cid}")
    @ApiOperation("获取优惠券")
    public Result getCoupon(@PathVariable("cid") Integer cid) {
        Coupon coupon = couponService.getById(cid);
        Assert.notNull(coupon, "找不到该优惠券,该优惠券已过期或已删除!");

        return Result.successResult(coupon);
    }

    /**
     * 通过用户Id获取 该用户所有优惠券
     * @param userId
     * @return
     */
    @GetMapping("getUserCoupon")
    @ApiOperation("获取用户优惠券")
    public Result getUserCoupon(Integer userId) {
        Result result = userClient.getUser(userId);
        User user = Result.getOriginal(result, User.class);
        Assert.notNull(user, "获取用户优惠券失败,查询不到该用户!");

        List<UserCoupon> list = userCouponService.list(new QueryWrapper<UserCoupon>().eq("user_id", userId));
        Assert.notNull(list, "查询用户优惠券失败!");

        return Result.successResult(list);
    }

    @PostMapping("sendCoupon")
    @ApiOperation("发送优惠券")
    public Result sendCoupon(@RequestBody UserCoupon userCoupon) {
        boolean b = userCouponService.save(userCoupon);
        Assert.isTrue(b, "发送优惠券失败");

        return Result.successResult(null);
    }

    @PutMapping("/lessCoupon")
    @ApiOperation("减少优惠券")
    public Result lessCoupon(int userCouponId, int lessNum) {
        UserCoupon userCoupon = userCouponService.getById(userCouponId);
        Integer userCouponNum = userCoupon.getUserCouponNum();
        boolean update = userCouponService.update(
                userCoupon.setUserCouponNum(userCouponNum - lessNum)
                , new UpdateWrapper<UserCoupon>().eq("user_coupon_id", userCouponId));
        if (!update) throw new CustomException(Result.failedResult(300, "优惠券更新失败"));
        return Result.successResult();
    }

    @PostMapping("editCoupon")
    @ApiOperation("编辑优惠券")
    public Result editCoupon(@RequestBody Coupon coupon) {
        boolean b = couponService.saveOrUpdate(coupon);
        Assert.isTrue(b, "编辑优惠券失败!");

        return Result.successResult(null);
    }

    @DeleteMapping("delCoupon")
    @ApiOperation("删除优惠券")
    public Result delCoupon(@RequestParam("couponId") Integer couponId) {
        boolean b = couponService.removeById(couponId);
        Assert.isTrue(b, "删除优惠券失败!");

        return Result.successResult(null);
    }


}
